<?php
/**
 * 商品详情页面
 * Created by Sublime Text 3.
 * @authors Rejeo (rejeo@qq.com)
 * @date    2018-05-06 23:02:54
 * @version $Id$
 */
namespace app\index\controller;

use app\index\controller\Base;
use think\Db;

class Detail extends Base
{
    public function index($id)
    {
        $flag = 0;
        $timedate = '';

        if (!intval($id)) {
            $this->error('ID不合法！');
        }
        // $field = ['id','name','bis_id','category_id','city_id','buy_count','total_count','location_ids','origin_price','current_price','image','end_time','coupons_begin_time','coupons_end_time','status'];
        // 根据id查找对应的商品
        // $deal = Db::name('deal')->field($field)->find($id);
        $deal = Db::name('deal')->find($id);
        // dump($deal);exit;
        if (!$deal || !$deal['status']) {
            $this->error('该商品不存在！');
        }

        // 获取栏目分类
        $category = Db::name('category')->find($deal['category_id']);

        // 获取分店信息
        $locations = model('BisLocation')->getNormalLocationsInId($deal['location_ids']);

        // 开始抢购处理
        if ($deal['start_time']>time()) {
            $flag = 1;
            $dtime = $deal['start_time']-time();
            $d = floor($dtime/(3600*24));
            $h = floor($dtime%(3600*24)/3600);
            $m = floor($dtime%(3600*24)%3600/60);
            if ($d) {
                $timedate .= $d.'天';
            }
            if ($h) {
                $timedate .= $h.'小时';
            }
            if ($m) {
                $timedate .= $m.'分';
            }
            $this->assign('timedate',$timedate);
        }
        $this->assign([
            'flag'      => $flag,
            'deal'      => $deal,
            'category'  => $category,
            'locations' => $locations,
            'overplus'  => ($deal['total_count']-$deal['buy_count']),
            'mapstr'    => $locations[0]['xpoint'].','.$locations[0]['ypoint'],
        ]);
        return $this->fetch();
    }
}
